package com.lvtu.monitor.module.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;


/** 
 * @Title: BaseAction.java 
 * @Package BaseController 
 * @Description: TODO 
 * @author dengcheng 
 * @date 2015-5-25 下午1:37:30 
 * @version V1.0.0 
 */
public class BaseAction implements Serializable {
	/** 
	 * @Fields serialVersionUID  
	 */ 
	private static final long serialVersionUID = 1L;
	
	private static Logger log = Logger.getLogger(BaseAction.class);

	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected Model model;
    /*
     * 被@ModelAttribute注释的方法会在此controller每个方法执行前被执行，
     * 因此对于一个controller映射多个URL的用法来说，要谨慎使用。
     */
	@ModelAttribute
	public void setReqAndResp(Model model,HttpServletRequest request,HttpServletResponse response){
		this.request = request;
		this.response = response;
		this.model = model;
	}
	
	
	
	
	
	
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public Model getModel() {
		return model;
	}

	public void setModel(Model model) {
		this.model = model;
	}
	
	protected void writeOutput(String output){
		if(response == null){
			log.info("Unable to get servlet response!");
			return;
		}
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.write(output);
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(out != null){
				out.close();
			}
		}
	}
}
